$("#head").load("header.html",function () {})
$("#foot").load("footer.html",function () {})
new Vue({
    el:'#search',
    data:{
        list1:false,
        list2:false,
        prompt:true,
        result:'',
        //公用url
        url:'http://api.456mobi.com/upload/',
        //数据
        block:[],
        //快讯保存
        newsFlash:[],
        //资讯保存
        information:[],
        news_list:[],//新闻列表
        news_list2:[],//新闻列表
        arr:[],
        choose:'choose',
        keyword:"",
        keyword_zx_count:"",
        keyword_kx_count:"",
    },
    ready:function(){
        var that = this;
        //回车搜索
        $(document).keyup(function(event) {
            if (event.keyCode == 13) {
                if($("#input").val()==""){
                    alert("请输入要搜索的内容")
                }else {
                    that.search("input")
                }
            }
        })
        $("#wx_icon").hover(function(){
            $("#wx_ii").show()
        },function(){
            $("#wx_ii").hide()
        });
        var get_keyword = getUrlParam('keyword');
        //alert(get_keyword);
        if(get_keyword!=null){

            that.keyword=get_keyword;
            that.search(that.keyword);
        }
        $('.tabs>div').click(function () {
            if($(this).html() == '资讯'){
                $('.tabs .arrow').css({'left':'40px'}).siblings("#zx").addClass("on").siblings("#kx").removeClass("on");
                if(that.block.length<1){
                    that.list1 = false
                    that.prompt = true
                    that.list2 = false
                }else{
                    that.list1 = true
                    that.prompt = false
                    that.list2 = false
                }
                $("#zx_c").show();
                $("#kx_c").hide();
            }else{
                $('.tabs .arrow').css({'left':'125px'}).siblings("#kx").addClass("on").siblings("#zx").removeClass("on");
                if(that.news_list.length<1){
                    that.list1 = false
                    that.prompt = true
                    that.list2 = false

                }else{
                    that.list1 = false
                    that.prompt = false
                    that.list2 = true
                }
                $("#zx_c").hide();
                $("#kx_c").show();
            }
        })
        var scr = function () {
            var winPos = $(window).scrollTop();
            if(winPos >= 400){
                $('#totop').css({'display':'inline-block'})
            }else{
                $('#totop').css({'display':'none'})
            }
        }
        $(window).scroll(function(){
            scr();
        })
        scr();
    },
    methods:{
        down : function(id,e){
            if($("#" + id).css('maxHeight') == '88px'){
                $("#" + id).css({'-webkit-line-clamp':'20'})
                $("#" + id).css({'maxHeight':'500px'})
                $('#' + e.target.id).css({"transform":"rotate(180deg)"})
            }else{
                $("#" + id).css({'-webkit-line-clamp':'3'})
                $("#" + id).css({'maxHeight':'88px'})
                $('#' + e.target.id).css({"transform":"rotate(0deg)"})
            }
        },
        bull:function (id) {
            var self= this
            var count= parseInt($("#bull"+id+" span").html());
            count+=1;
            $("#bull"+id+" span").html(count);

            $.ajax({
                url :"http://api.456mobi.com/api/?s=home/Information/evaluate",
                type : 'post',
                data : {
                    newsflash_id : id,
                    sign:'bull_vote',
                },
                success: function(ret){
                    count = parseInt(ret.data.bull_vote);
                    var bad = parseInt(ret.data.bad_vote);
                    $("#bull"+id+" span").html(count);
                    $("#bad"+id+" span").html(bad);

                    $("#bad"+id+" .li_kong1").hide();
                    $("#bad"+id+" .li_kong4").hide();
                    $("#bad"+id+" .li_kong2").hide();
                    $("#bad"+id+" .li_kong3").show();

                    if(ret.data.user_data.bull_vote == 1){
                        $("#bull"+id+" .li_hao1").hide();
                        $("#bull"+id+" .li_hao2").hide();
                        $("#bull"+id+" .li_hao3").hide();
                        $("#bull"+id+" .li_hao4").show();
                    }else{
                        $("#bull"+id+" .li_hao4").hide();
                        $("#bull"+id+" .li_hao2").hide();
                        $("#bull"+id+" .li_hao1").hide();
                        $("#bull"+id+" .li_hao3").show();
                    }
                }
            })
        },
        bad:function (id) {
            var count = parseInt($("#bad"+id+" span").html());
            count+=1;
            $("#bad"+id+" span").html(count);
            $("#bad"+id+" .li_kong2").show();
            $("#bad"+id+" .li_kong1").hide();
            $.ajax({
                url :"http://api.456mobi.com/api/?s=home/Information/evaluate",
                type : 'post',
                data : {
                    newsflash_id : id,
                    sign:'bad_vote',
                },
                success: function(ret){
                    count = parseInt(ret.data.bad_vote);
                    var bull = parseInt(ret.data.bull_vote)
                    $("#bad"+id+" span").html(count);
                    $("#bull"+id+" span").html(bull);
                    $("#bull"+id+" .li_hao1").hide();
                    $("#bull"+id+" .li_hao4").hide();
                    $("#bull"+id+" .li_hao2").hide();
                    $("#bull"+id+" .li_hao3").show();

                    if(ret.data.user_data.bad_vote == 1){
                        $("#bad"+id+" .li_kong1").hide();
                        $("#bad"+id+" .li_kong3").hide();
                        $("#bad"+id+" .li_kong2").hide();
                        $("#bad"+id+" .li_kong4").show();
                    }else{
                        $("#bad"+id+" .li_kong4").hide();
                        $("#bad"+id+" .li_kong1").hide();
                        $("#bad"+id+" .li_kong2").hide();
                        $("#bad"+id+" .li_kong3").show();
                    }
                }
            })
        },
        more:function () {
            var that = this
            $.ajax({
                url:'http://api.456mobi.com/api/?s=home/Information/search_info',
                type:'post',
                data:{
                    type : 1,
                    keyword: that.result.data.keyword,
                    id : that.block[that.block.length-1].id
                },
                success: function(response){
                    var res = response
                    //console.log(res)
                    if(res.msg == '获取成功'){
                        res.data.list.forEach(function (item,index) {
                            item.more = JSON.parse(item.more)
                            var time = new Date(item.published_time * 1000).toLocaleString().split(' ')
                            time[0] = time[0].replace(/\//g,"-").substr(5,time[0].length)
                            time[1] = time[1].substring(2,time[1].length)
                            time[1] = time[1].split(':')
                            item.published_time = time[0] + ' ' + time[1][0] + ':' + time[1][1];
                            item.post_title = item.post_title.replace(new RegExp(res.data.keyword,'g'),'<span>'+res.data.keyword+'</span>');
                            item.post_title = item.post_excerpt.replace(new RegExp(res.data.keyword,'g'),'<span>'+res.data.keyword+'</span>')
                            that.block.push(item)
                        })
                    }else if (res.msg=='暂无更多'){
                        $('.btn').css({'color':'#aaa'})
                        $('.btn').html('暂无更多')
                    }else{
                        // alert(res.msg)
                    }
                }
            })
        },
        newsmore:function () {
            var self = this;
            //alert(self.news_list[self.news_list.length-1].news_list2[self.news_list[self.news_list.length-1].news_list2.length-1].id)
            $.ajax({
                url:'http://api.456mobi.com/api/?s=home/Information/search_info',
                type:'post',
                data:{
                    type : 2,
                    keyword: self.result.data.keyword,
                    id :self.news_list[self.news_list.length-1].news_list2[self.news_list[self.news_list.length-1].news_list2.length-1].id
                },
                success: function(ret){
                    var arr = ret.data.list;
                    if(ret.msg == '获取成功') {
                        $.each(arr, function (index, val) {
                            var data = {};
                            var timestamp = Date.parse(index);
                            data.issue_time = self.timestampToTime_year(timestamp / 1000);
                            data.date_str = self.timestampToTime_is(index);
                            data.week = self.timestampToTime_week(timestamp / 1000);
                            if (self.news_list[self.news_list.length - 1].issue_time != data.issue_time) {
                                // alert(self.news_list[self.news_list.length-1].issue_time+"//"+data.issue_time)
                                self.news_list.push(data)
                                Vue.set(self.news_list[self.news_list.length - 1], 'news_list2', [])
                            }
                            $.each(val, function (index2, val2) {
                                var data2 = {}
                                data2.id = val2.id;
                                data2.newsflash_id = val2.newsflash_id;
                                data2.title = val2.title.replace(new RegExp(self.result.data.keyword, 'g'), '<span>' + self.result.data.keyword + '</span>')
                                data2.content = val2.content.replace(new RegExp(self.result.data.keyword, 'g'), '<span>' + self.result.data.keyword + '</span>')
                                data2.source = val2.source;
                                data2.issue_time = val2.issue_time;
                                data2.bad_vote = val2.bad_vote;
                                data2.bull_vote = val2.bull_vote;
                                data2.evaluate = val2.evaluate;
                                // self.news_list[self.news_list.length-1].news_list2.push(data2);
                                Vue.set(self.news_list[self.news_list.length - 1].news_list2, self.news_list[self.news_list.length - 1].news_list2.length, data2)
                            })
                        })
                    }else if (ret.msg=='暂无更多'){
                        $('.btns').css({'color':'#aaa'})
                        $('.btns').html('暂无更多')
                    };
                }
            })
        },
        timestampToTime_year :function (timestamp) {
            var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            Y = date.getFullYear() + '-';
            M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            D = date.getDate() + ' ';
            h = date.getHours() + ':';
            m = date.getMinutes() + ':';
            s = date.getSeconds();
            return Y+M+D;
        },
        timestampToTime_fs :function (timestamp) {
            var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            Y = date.getFullYear() + '-';
            M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            D = date.getDate() + ' ';
            h = date.getHours() + ':';
            m = date.getMinutes() + '';
            s = date.getSeconds();
            return h+m;
        },
        timestampToTime_is:function (timestamp) {
            var self = this;
            var now = new Date();
            var time = new Date(new Date(timestamp));
            D = now.getDate();
            D2 = time.getDate();
            if(D==D2){
                return "今天"
            }else if(D>D2){
                if(D-D2>2){
                    return ""
                }
                return "昨天"
            }

        },
        timestampToTime_week :function (timestamp) {
            var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            W= date.getDay();
            if(W==0)  W="星期日"
            if(W==1)  W="星期一"
            if(W==2)  W="星期二"
            if(W==3)  W="星期三"
            if(W==4)  W="星期四"
            if(W==5)  W="星期五"
            if(W==6)  W="星期六"
            return W;
        },
        search:function (input) {
            var that = this;
            var self = this;
            var keywords = "";
           //alert(that.keyword);
            //return false;
            /*if(that.keyword==null){
                if($('#input').val() == ''){
                    alert('请输入搜索内容')
                    return;
                }
                else{
                    keywords = $('#input').val();
                }
            }*/
            if(input=="input"){
                keywords = $('#input').val();
            }
            if(input!="input"){
                keywords = input;
            }
            if(input==null){
                alert("请输入关键字！")
                return false;
            }
           // alert(keywords)
            that.block = []
            that.news_list = []
            that.arr = []
            $.ajax({
                url:'http://api.456mobi.com/api/?s=home/Information/search_all',
                type:'post',
                data:{
                    keyword:keywords
                },
                success: function(response){
                    if(response.code == '1'){
                        var res = response
                        that.result = response;
                        $(".tab_box p").show();
                        that.keyword=res.data.keyword;
                        that.keyword_zx_count=res.data.zx.count;
                        that.keyword_kx_count=res.data.kx.count;
                        if(that.keyword_zx_count==0){
                            $(".btn").hide();
                        }else{
                            $(".btn").show();
                        }
                        if(that.keyword_kx_count==0){
                            $(".btns").hide();
                        }else{
                            $(".btns").show();
                        }
                        console.log(response)
                        res.data.zx.list.forEach(function (item,index) {
                            //资讯
                            item.more = JSON.parse(item.more)
                            var time = new Date(item.published_time * 1000).toLocaleString().split(' ')
                            time[0] = time[0].replace(/\//g,"-").substr(5,time[0].length)
                            time[1] = time[1].substring(2,time[1].length)
                            time[1] = time[1].split(':')
                            item.published_time = time[0] + ' ' + time[1][0] + ':' + time[1][1];
                            item.post_title = item.post_title.replace(new RegExp(res.data.keyword,'g'),'<span>'+res.data.keyword+'</span>')
                            item.post_excerpt = item.post_excerpt.replace(new RegExp(res.data.keyword,'g'),'<span>'+res.data.keyword+'</span>');
                            console.log(item)
                        })
                        that.block = res.data.zx.list;
                        //var regexKeyword = new RegExp($(".right p").eq(0).text(), that.keyword);
                       // $(".right p").eq(0).html(str.replace(regexKeyword, "<span class='highlight'>" + regexKeyword.source + "</span>"));
                        //console.log(res.data.zx.list);
                        $.each(res.data.kx.list, function(index,val) {
                            //快讯
                            var data = {};
                            var timestamp = Date.parse(index);
                            data.issue_time = self.timestampToTime_year(timestamp/1000);
                            data.date_str = self.timestampToTime_is(index);
                            self.arr.push(index);
                            var length=self.arr.length;
                            if(self.arr[length-2]!=index){
                                $(".news_list_box span.append").append("<p>"+index+"</p>");
                            }
                            data.week = self.timestampToTime_week(timestamp/1000);
                            self.news_list.push(data);
                            self.news_list.forEach(function (item,index) {
                                Vue.set(self.news_list[index],'news_list2',[])
                               // console.log(self.news_list)
                                $.each(val, function(index2,val2) {
                                    var data2 = {}
                                    data2.id = val2.id;
                                    data2.newsflash_id = val2.newsflash_id;
                                    data2.title = val2.title.replace(new RegExp(res.data.keyword,'g'),'<span>'+res.data.keyword+'</span>');
                                    data2.content = val2.content.replace(new RegExp(res.data.keyword,'g'),'<span>'+res.data.keyword+'</span>');
                                    data2.source = val2.source;
                                    data2.issue_time = val2.issue_time;
                                    data2.bad_vote = val2.bad_vote;
                                    data2.bull_vote = val2.bull_vote;
                                    data2.evaluate = val2.evaluate;
                                  //  console.log(data2);
                                    item.news_list2.push(data2);
                                })
                            })

                        });
                        if(that.block.length<1&&that.news_list.length<1){
                            that.list1 = false;
                            that.list2 = false;
                            that.prompt = true;
                        }else{
                            if($('.tabs .arrow').css('left') == '40px'){
                                if(that.block.length<1){
                                    that.list1 = false
                                    that.list2 = false
                                    that.prompt = true
                                }else{
                                    that.list1 = true
                                    that.list2 = false
                                    that.prompt = false
                                }
                                $("#zx_c").show();
                                $("#kx_c").hide();
                            }else{
                                if(that.news_list.length<1){
                                    that.list1 = false
                                    that.list2 = false
                                    that.prompt = true
                                }else{
                                    that.list1 = false
                                    that.list2 = true
                                    that.prompt = false

                                }
                                $("#zx_c").hide();
                                $("#kx_c").show();
                            }
                        }
                    }else{
                        $(".tab_box p").hide();
                        $(".btn").hide();
                        $(".btns").hide();
                        alert('资讯获取失败')
                    }
                }
            })
        },
        news_details:function (id,img_url) {
            $(location).attr('href', 'news_details.html?id='+id+'&img_url='+img_url);
        },
    }
});